Parameters m.Servico

Private aliold, ordold1, ordold2, regold1, regold2, ltalk

if Set('Talk') = 'ON'
   set talk off
   ltalk = .t.
  else
   ltalk = .f.
endif

m.aliold  = select()

Select Servicos
       m.ordold1 = order()
       m.regold1 = recno()

Select SubServ
       m.ordold2 = order()
       m.regold2 = recno()

Dimension vSer(1)

m.Nivel   = 0
m.NumSer  = 1
m.ciclico = .f.

do VerSub with m.Servico

Select Servicos
       set order to tag &ordold1
       
if between(m.regold1, 1, reccount() )
   goto (m.regold1)
endif

Select SubServ
       set order to tag &ordold2
       
if between(m.regold2, 1, reccount() )
   goto (m.regold2)
endif

Select (m.AliOld)

if ltalk
   set talk on
endif

return (m.ciclico)



Procedure   VerSub
Parameters  m.SubServ

Private m.regmov

Select Servicos
       set order to tag Codigo

if m.Nivel >= 25
   m.nivel = m.nivel - 1
   return
endif

m.nivel = m.nivel + 1

Dimension vSer(m.Nivel)

vSer(m.Nivel) = m.SubServ

for ind = 1 to m.Nivel-1
    if m.SubServ = vSer(Ind)
       m.Ciclico = .t.
       m.nivel = m.nivel - 1
       return
    endif
endfor

Seek m.SubServ

if Servicos.TotSub > 0

   Select SubServ
          set order to tag Servico
          
   Seek m.SubServ
   
   scan while Servico = m.SubServ
   
        m.regmov = recno()

        do VerSub with SubServ.SubServico
        
        if m.ciclico
           exit
        endif        
        
        Select SubServ
        goto (m.RegMov)
        
   endscan
   
endif

m.nivel = m.nivel - 1

